//
//  F_BankCardCell.swift
//  letuofu
//
//  Created by WH-JS on 2020/3/13.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import UIKit

class F_BankCardCell: UITableViewCell {
    let iconImg = UIImageView()
    let labTitle = UILabel()
    let labCardNO = UILabel()
    let labMoney = UILabel()
    let showButton = Button()
    
    var data : [String : Any?]? {
        didSet{
            loadData()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    func getBankCardDetailInfo(callback: @escaping ([String:Any?]) -> Void) {
        let params :[String:Any] = ["identityToken":LocalStorage.getString(forKey: USER_IDENTITYTOKEN) ?? "",
                                    "bankCardIdentification":self.data?.getString("bankCardIdentification") ?? ""]
        
        API.request(url: .F_GetBankCardDetailInfo, params: params) { (result) in
            if result.success {
               if let dl = result.data?.getDictionary("data") {
                callback(dl)
               }
            }else{
            }
        }
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func setupUI() {
        self.backgroundColor = .clear
        let bgview = self.drawView { (make) in
            make.width.equalToSuperview().offset(-20)
            make.height.equalToSuperview().offset(-10)
            make.center.equalToSuperview()
        }
        bgview.backgroundColor = .white
        bgview.layer.cornerRadius = 3
        
        bgview.drawImage(imageView: iconImg, named: "bank_icon") { (make) in
            make.top.equalToSuperview().offset(15)
            make.left.equalToSuperview().offset(13)
        }
        bgview.drawLabel(label: labTitle,text: "", fontSize: 17, textColor: .x333333) { (make) in
            make.top.equalToSuperview().offset(15)
            make.left.equalToSuperview().offset(35)
        }
        bgview.drawLabel(label: labCardNO,text: "", fontSize: 17, textColor: .x333333) { (make) in
            make.top.equalToSuperview().offset(50)
            make.centerX.equalToSuperview()
        }
        
        //画横线
        let line = bgview.drawView { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview().offset(-29)
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
        
        bgview.drawLabel(label: labMoney,text: "余额: ***** 元", fontSize: 14, textColor: .x333333) { (make) in
            make.bottom.equalToSuperview().offset(-7)
            make.left.equalToSuperview().offset(13)
        }
        bgview.drawButton(button: showButton,imageNamed: "eyes_c") { (make) in
            make.right.equalToSuperview().offset(-13)
            make.bottom.equalToSuperview().offset(-10)
        }
        showButton.hitEdgInsets = .init(top: -20, left: -20, bottom: -20, right: -20)
        showButton.setImage(UIImage(named: "eyes"), for: .selected)
        showButton.addTarget(self, action: #selector(_moneyBtnClick(btn:)), for: .touchUpInside)
    }
    
    func loadData() {
        showButton.isSelected = false
        labMoney.text = "余额: ***** 元"
        labTitle.text = data?.getString("bankName")
        let cardno = (data?.getString("bankCardNo")?.suffix(4) ?? "****")
        labCardNO.text = "****     ****     ****     " + String(cardno)
    }
    
    @objc func _moneyBtnClick(btn: Button) {
        showButton.isSelected = !showButton.isSelected
        if showButton.isSelected {
//            if let bankCardBalance = data?.getString("bankCardBalance") {
//                self.labMoney.text = "余额:" + bankCardBalance + " 元"
//                return
//            }
            getBankCardDetailInfo(callback: { (data) in
//                self.data?.updateValue(data.getString("bankCardBalance"), forKey: "bankCardBalance")
                self.labMoney.text = "余额:" + (data.getString("bankCardBalance") ?? "") + " 元"
            })
        }
        else {
            labMoney.text = "余额: ***** 元"
        }
    }
}
